package 笔试.皇甫;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author:谢君臣
 * @Date:2021/9/26-15:32
 * @version:1.0
 * @function:
3 5
08:00-09:00
09:30-11:00
13:00-15:00
07:00-08:00
08:00-11:00
12:00-13:30
14:00-17:00
18:00-19:00
 */
public class Main01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int m = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        List<double[]> list1 = new ArrayList<>();
        List<double[]> list2 = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            String[] time = scanner.nextLine().split("-");
            double start = fun(time[0]);
            double end = fun(time[1]);
            list1.add(new double[]{start, end});
        }
        for (int i = 0; i < n; i++) {
            String[] time = scanner.nextLine().split("-");
            double start = fun(time[0]);
            double end = fun(time[1]);
            list2.add(new double[]{start, end});
        }
        //选择最早时间结束的
        int count = 0, i = 0, j = 0; double start = 0;
        while (i < list1.size() && j < list2.size()) {
            while (i < list1.size() && list1.get(i)[0] < start) i++;
            while (j < list2.size() && list2.get(j)[0] < start) j++;
            if (i < list1.size() && j < list2.size()){
                start = Math.min(list1.get(i)[1],list2.get(j)[1]);
                count++;
            }else if(i < list1.size() || j < list2.size()){
                count = count+ list1.size()-i + list2.size()-j;
                break;
            }
        }
        System.out.println(count);
    }
    private static double fun(String s) {
        double res = 0.0;
        String[] split = s.split(":");
        res = Integer.parseInt(split[0]) + Integer.parseInt(split[1]) / 60.0;
        return res;
    }

}
